﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tecnomatix.Engineering;

namespace autoCheck
{
    public enum Type { TxPlcInputSignal, TxPlcOutputSignal, TxPlcLightSensorSignal }
    public class PlcOperation
    {
       
        public static int ReadPlcValue(string signalName, Type type, string objName)
        {
            ITxTypeFilter filter = new TxNoTypeFilter();
            //按照项目名称获取主程序
            TxObjectList plcPrograms = TxApplication.ActiveDocument.GetObjectsByName(objName);
            for (int i = 0; i < plcPrograms.Count; i++)
            {
                //检测获取对象是否为TxPlcProgram
                if (plcPrograms[i].GetType().FullName.Equals("Tecnomatix.Engineering.TxPlcProgram"))
                {
                    TxPlcProgram plcProgram = plcPrograms[i] as TxPlcProgram;
                    //取出所有关联在主程序TxPlcProgram下的所有类型的信号
                    TxObjectList list = plcProgram.GetSignals(filter);

                    foreach (var item in list)
                    {
                        if (type==0)
                        {
                            if ("Tecnomatix.Engineering.TxPlcInputSignal".Equals(item.GetType().ToString()))
                            {

                                TxPlcInputSignal signal = item as TxPlcInputSignal;
                                if (signalName.Equals(signal.IECFormatAddress))
                                {
                                    if (signal.Value.BooleanValue)
                                    {
                                        return 1;
                                    }
                                    else { return 0; }

                                }
                            }
                        }
                        else if ((int)type==1)
                        {
                            if ("Tecnomatix.Engineering.TxPlcOutputSignal".Equals(item.GetType().ToString()))
                            {
                                
                                var signal = item as TxPlcOutputSignal;
                                if (signalName.Equals(signal.IECFormatAddress))
                                {
                                    if (signal.Value.BooleanValue)
                                    {
                                        return 1;
                                    }
                                    else { return 0; }
                                }
                            }
                        }
                        else if ((int)type==2)
                        {
                            if ("Tecnomatix.Engineering.TxPlcLightSensorSignal".Equals(item.GetType().ToString()))
                            {

                                TxPlcLightSensorSignal signal = item as TxPlcLightSensorSignal;
                                if (signalName.Equals(signal.IECFormatAddress))
                                {
                                    if (signal.Value.BooleanValue)
                                    {
                                        return 1;
                                    }
                                    else { return 0; }
                                }
                            }
                        }
                    }
                }
            }
            return -1;
        }
        public static void ChangePlcValue(string signalName, bool value, string objName)
        {

            ITxTypeFilter filter = new TxNoTypeFilter();
            //按照项目名称获取主程序
            TxObjectList plcPrograms = TxApplication.ActiveDocument.GetObjectsByName(objName);
            for (int i = 0; i < plcPrograms.Count; i++)
            {
                //检测获取对象是否为TxPlcProgram
                if (plcPrograms[i].GetType().FullName.Equals("Tecnomatix.Engineering.TxPlcProgram"))
                {
                    TxPlcProgram plcProgram = plcPrograms[i] as TxPlcProgram;
                    //取出所有关联在主程序TxPlcProgram下的所有类型的信号
                    TxObjectList list = plcProgram.GetSignals(filter);

                    foreach (var item in list)
                    {
                        if ("Tecnomatix.Engineering.TxPlcInputSignal".Equals(item.GetType().ToString()))
                        {

                            TxPlcInputSignal signal = item as TxPlcInputSignal;
                            if (signalName.Equals(signal.IECFormatAddress))
                            {
                                var v = new TxPlcValue
                                {
                                    BooleanValue = value
                                };
                                signal.Value = v;
                            }
                        }
                    }
                }
            }
        }
    }
}
